package com.face.server.controller;

import com.face.RespMsg;

import com.face.server.dao.JoinedDAO;
import com.face.server.repository.CameraRepository;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.repository.DormitoryRepository;

import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.service.CSVService;

import com.face.server.service.DormitoryBuildService;
import com.face.server.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.Principal;


/**
 * Created by ryan on 2018/3/13.
 */
@RestController
@RequestMapping("/dormBuild")
public class DormitoryBuildController {

    /* 这里直接用了dao层 */
    @Autowired
    DormitoryBuildRepository dormitoryBuildRepository;

    @Autowired
    CSVService csvService;

    @Autowired
    private DormitoryBuildService dormitoryBuildService;

    @GetMapping
    RespMsg all() {
        return RespMsg.build(dormitoryBuildRepository.findAll());
    }

    @PostMapping
    RespMsg batchSave(MultipartFile file) throws IOException {
        return file == null ?
                RespMsg.error("对不起，文件不能为空") :
                csvService.parse(file);
    }



    @Autowired
    private CameraRepository cameraRepository;

    @Autowired
    private DormitoryService dormitoryService;

    @GetMapping("/countCams")
    public RespMsg countCams(){
        return RespMsg.build(cameraRepository.count());
    }

    @GetMapping("/countStuInDorm")
    public RespMsg countStuInDorm(){
        return RespMsg.build(dormitoryBuildService.countStuInDorm());
    }

    @GetMapping("/countDormOccupied")
    public RespMsg countDormOccupied(){
        return RespMsg.build(dormitoryBuildService.countDormOccupied());
    }

    @GetMapping("/countVisitors")
    public RespMsg countVisitors(){
        return RespMsg.build(dormitoryBuildService.countVisitors());
    }

    @GetMapping("/listDormBuildInfo")
    public RespMsg listDormBuildInfo(@PageableDefault(sort = "buildingNum",size = 10000) Pageable pageable, @RequestParam(required = false) Integer buildNum){
        return RespMsg.build(dormitoryBuildService.listDormBuildInfo(pageable,buildNum));
    }

    @GetMapping("/listMyDormBuildInfo")
    public RespMsg listMyDormBuildInfo(@PageableDefault(sort = "buildingNum",size = 10000)Pageable pageable, @RequestParam(required = false)Integer buildNum, Principal principal){
        return RespMsg.build(dormitoryBuildService.listDormBuildInfo(pageable,buildNum,principal));
    }
}
